﻿using MesBSW.DAL.MesDB;
using MesBSW.DAL.MesDTO;
using MesBSW.Tools;
using MesBSW.View;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MesBSW.BLL.Mes
{
    public class LanguageControler : BaseControler
    {

        /// <summary>
        /// 根据用户选择的语言取得该类语言下面所有已有的翻译项目
        /// </summary>
        /// <param name="infoBean"></param>
        /// <returns></returns>
        public List<LanguageMulitDTO> SearchLanguageMulitForType(LanguageInfo infoBean)
        {
            List<LanguageMulitDTO> resList = new List<LanguageMulitDTO>();
            using (MesDB ctx = new MesDB())
            {
                var query = from a in ctx.LanguageMulits
                            join b in ctx.LanguageInfos on a.lang_info_id equals b.id into res1
                            from ab in res1
                            join c in ctx.LanguageItems on a.lang_item_id equals c.id into res2
                            from ac in res2
                            select new { A = a, AB = ab, AC = ac };

                if (infoBean != null && infoBean.id > 0)
                {
                    query = query.Where(q => q.AB.id == infoBean.id);
                }

                if (query != null && query.Count() > 0)
                {
                    foreach (var item in query)
                    {
                        LanguageMulitDTO lmDto = new LanguageMulitDTO();
                        EntityTools.CopyEntity<LanguageMulit, LanguageMulitDTO>(item.A, ref lmDto);

                        lmDto.LanguageInfos = item.AB;
                        lmDto.LanguageItems = item.AC;
                        resList.Add(lmDto);
                    }
                }
            }
            return resList;
        }

        /// <summary>
        /// 根据条件取得多语言的翻译资料
        /// </summary>
        /// <param name="langCode">语言种类</param>
        /// <param name="formName">界面完整类名</param>
        /// <param name="ctrlName">控件名称</param>
        /// <returns></returns>
        public string SearchDispTextForCondition(string langCode, string formName, string ctrlName)
        {
            string resStr = "";
            using (MesDB ctx = new MesDB())
            {
                var query = from a in ctx.LanguageMulits
                            join b in ctx.LanguageInfos on a.lang_info_id equals b.id into res1
                            from ab in res1
                            join c in ctx.LanguageItems on a.lang_item_id equals c.id into res2
                            from ac in res2
                            select new { A = a, AB = ab, AC = ac };

                if (string.IsNullOrEmpty(langCode) == false)
                {
                    query = query.Where(q => q.AB.lang_code == langCode);
                }

                if (string.IsNullOrEmpty(formName) == false)
                {
                    query = query.Where(q => q.AC.form_name == formName);
                }

                if (string.IsNullOrEmpty(ctrlName) == false)
                {
                    query = query.Where(q => q.AC.ctrl_name == ctrlName);
                }

                resStr = query != null && query.Count() > 0 ? query.First().A.lang_text : "";
            }
            return resStr;
        }


        /// <summary>
        /// 保存多语言翻译项目
        /// </summary>
        /// <param name="saveList"></param>
        /// <returns></returns>
        public int SaveLanguageItem(List<LanguageItemDTO> saveList)
        {
            int count = 0;
            DateTime tmpDay = this.GetCurrDateTime();
            using (MesDB ctx = new MesDB())
            {
                var tran = ctx.Database.BeginTransaction();
                try
                {
                    foreach (LanguageItemDTO itemBean in saveList)
                    {
                        LanguageItem tmpItemBean = new LanguageItem();
                        EntityTools.CopyEntity<LanguageItemDTO, LanguageItem>(itemBean, ref tmpItemBean);
                        if (tmpItemBean.id != 0)
                        {
                            tmpItemBean.update_date = tmpDay;
                            tmpItemBean.update_user = MainFrm.LoginUser.user_name;
                            ctx.LanguageItems.Attach(tmpItemBean);
                            ctx.Entry(tmpItemBean).State = System.Data.Entity.EntityState.Modified;
                        }
                        else
                        {
                            tmpItemBean.create_date = tmpDay;
                            tmpItemBean.create_user = MainFrm.LoginUser.user_name;
                            ctx.LanguageItems.Add(tmpItemBean);
                        }

                        count = count + ctx.SaveChanges();

                        if (itemBean.LanguageMulits != null && itemBean.LanguageMulits.Count > 0)
                        {
                            foreach (LanguageMulit mulitBean in itemBean.LanguageMulits)
                            {
                                if (mulitBean.id != 0)
                                {
                                    mulitBean.update_date = tmpDay;
                                    mulitBean.update_user = MainFrm.LoginUser.user_name;
                                    ctx.LanguageMulits.Attach(mulitBean);
                                    ctx.Entry(mulitBean).State = System.Data.Entity.EntityState.Modified;
                                }
                                else
                                {
                                    mulitBean.lang_item_id = tmpItemBean.id;
                                    mulitBean.create_date = tmpDay;
                                    mulitBean.create_user = MainFrm.LoginUser.user_name;
                                    ctx.LanguageMulits.Add(mulitBean);
                                }
                                count = count + ctx.SaveChanges();
                            }
                        }
                    }

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();

                    throw ex;
                }
            }
            return count;
        }


        /// <summary>
        /// 取得语言种类的最新的代号
        /// </summary>
        /// <returns></returns>
        public string GetMaxCodeForLanguageInfo()
        {
            string newCode = "";
            using (MesDB ctx = new MesDB())
            {
                var query = ctx.LanguageInfos.AsQueryable().OrderByDescending(q => q.lang_code);
                if (query != null && query.Count() > 0)
                {
                    string tmpStr = query.First().lang_code;
                    newCode = $"Lang{(int.Parse(tmpStr.Substring(4, 5)) + 1).ToString("D5")}";
                }
                else
                {
                    newCode = $"Lang00001";
                }
            }
            return newCode;
        }


        /// <summary>
        /// 保存多语言种类
        /// </summary>
        /// <param name="saveEntity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int AddLanguageInfo(LanguageInfo saveEntity)
        {
            int count = 0;
            DateTime tmpDay = this.GetCurrDateTime();
            using (MesDB ctx = new MesDB())
            {
                if (saveEntity is null)
                {
                    throw new Exception("传入的多语言种类为空！");
                }
                else if (string.IsNullOrEmpty(saveEntity.lang_code))
                {
                    throw new Exception("新增的多语言种类的代号为空！");
                }
                else if (string.IsNullOrEmpty(saveEntity.lang_name_cn))
                {
                    throw new Exception("新增的多语言种类的名称为空！");
                }
                else
                {
                    var exQuery = ctx.LanguageInfos.Where(q => q.lang_code == saveEntity.lang_code || q.lang_name_cn == saveEntity.lang_name_cn);
                    if (exQuery != null && exQuery.Count() > 0)
                    {
                        throw new Exception("新增的多语言种类的代号或名称已经存在！");
                    }
                    else
                    {
                        ctx.LanguageInfos.Add(saveEntity);
                        count = ctx.SaveChanges();
                    }
                }
            }
            return count;
        }
    }
}
